<form class="layui-form seller-form"  action="" >
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label" style="width:120px;">优惠券名称：</label>
            <div class="layui-input-inline seller-inline-3">
                <input type="text" name="name" lay-verify="title" placeholder="请输入优惠券名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">启用状态：</label>
            <div class="layui-input-inline seller-inline-2">
                <select name="status" lay-verify="">
                    <option value=""></option>
                    <option value="1">开启</option>
                    <option value="2">关闭</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">起止时间：</label>
            <div class="layui-input-inline seller-inline-6">
                <input  type="text" name="date" value="" id="date" placeholder="请输入起止时间" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <button class="layui-btn layui-btn-sm" lay-submit lay-filter="*"><i class="iconfont icon-chaxun"></i>筛选</button>
            <a href="{:url('Promotion/couponAdd')}" class="layui-btn layui-btn-sm option-add">
                <i class="layui-icon">&#xe608;</i> 添加
            </a>
        </div>
    </div>
</form>

<div class="table-body">
    <table id="promotion" lay-filter="test"></table>
</div>

<script>
    layui.use(['form', 'layedit', 'laydate','table'], function(){
        //时间插件
        layui.laydate.render({
            elem: '#date',
            range: '到',
            type: 'datetime'
        });

        //表格渲染
        var promotionTable = layui.table.render({
            elem: '#promotion',
            height: 'full-115',
            cellMinWidth: '80',
            page: 'true',
            limit:'{:config("zlserver.page_limit")}',
            url: "{:url('promotion/coupon')}?_ajax=1",
            id:'promotion',
            cols: [[
                {type:'numbers'},
                {field:'name',title:'优惠券名称'},
                {field:'sort', width:140, sort: true,title:'权重'},
                {field:'status', width:100,title:'启用状态', templet: '#status'},
                {field:'stime',width:170,title:'开始时间'},
                {field:'etime',width:170,title:'结束时间'},
                {field: 'operating', title: '操作', width:220, align: 'center',templet:function(data){
                    var html = '';
                    html += '<a href="{:url(\'Coupon/index\')}?id=' + data.id +'" class="layui-btn layui-btn-xs layui-btn-primary">券码列表</a>'
                    html += '<a  class="layui-btn layui-btn-xs" href="{:url('promotion/couponEdit')}?id=' + data.id + '">编辑</a>';
                    html += '<a  class="layui-btn layui-btn-xs option-upload" data-id="' + data.id + '" >下载</a>';
                    html += '<a  class="layui-btn layui-btn-danger layui-btn-xs option-del" data-id="' + data.id + '" >删除</a>';
                    return html;
                }}
            ]]
        });
        layui.form.on('submit(*)', function(data){
            layui.table.reload('promotion', {
                where: data.field
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });


        //促销删除
        $(document).on('click','.option-del',function(){
            var id = $(this).attr('data-id');
            layer.confirm('您确定删除此优惠券信息？', {
                btn: ['确认','取消'] //按钮
            }, function(){
                //去删除
                JsPost("{:url('promotion/couponDel')}",{'id':id},function(res){
                    if(res.status){
                        layer.msg('删除成功');
                        promotionTable.reload();
                    }else{
                        layer.msg(res.msg);
                    }
                });
            }, function(){
                layer.close(1);
            });
        });
        //优惠券下载
        $(document).on('click','.option-upload',function(){
            var tpl = '<form class="layui-form export-form"  action="" >'+
                    '<div class="layui-form-item">'+
                    '<div class="layui-margin-10">'+
                    '<blockquote class="layui-elem-quote layui-text">'+
                    '请输入导出数量，最多可以导出5000张'+
                    '</blockquote>'+
                    '</div>'+
                    '<div class="layui-inline">'+
                    '<label class="layui-form-label">导出数量：</label>'+
                    '<input type="text" name="nums" id="nums" lay-verify="title" style="width:100px;" placeholder="请输入数量" autocomplete="off" class="layui-input">'+
                    '</div>'+
                    '<div class="layui-inline">'+
                    '<label class="layui-form-label">任务名称：</label>'+
                    '<input type="text" name="taskname" id="taskname" lay-verify="title" style="width:200px;" placeholder="请输入任务名称" autocomplete="off" class="layui-input">'+
                    '</div>'+
                    '</div>'+
                    '</form>';
            var id = $(this).attr('data-id');
            layer.open({
                type: 1,
                title:'优惠券导出',
                area: ['400px', '300px'], //宽高
                btn:['确定','取消'],
                content: tpl,
                yes:function () {
                    var nums = $('#nums').val();
                    var taskname = $('#taskname').val();
                    var filter = "nums="+nums+"&id="+id;
                    var data = {
                        'filter': filter,
                        'taskname': taskname,
                        'model': "Coupon"
                    };
                    JsPost("{:url('Ietask/export')}",data,function(res){
                                layer.msg(res.msg, {time:1500}, function(){
                                    if(res.status){
                                        layer.closeAll();
                                        //layer.close(index);
                                    }
                                });
                            }
                    );
                },btn2:function () {
                    layer.closeAll();
                }
            });
        });


        //监听 操作状态
        layui.form.on('switch(change)', function(obj){
            JsPost("{:url('Promotion/changeState')}",{
                id: obj.value,
                elem: obj.elem.name,
                state: obj.elem.checked
            },function(res){
                layer.msg(res.msg);
            });
        });
    });
</script>

<script type="text/html" id="status">
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="change" {{ d.status == 1 ? 'checked' : '' }}>
</script>
